Add "Dry Run" to the action menu

Akinori MUSHA 9 years ago
parent
commit
15378aa006

+ 26 - 0
app/assets/javascripts/components/utils.js.coffee

@@ -33,3 +33,29 @@ class @Utils
33 33
       onHide?()
34 34
     body?(modal.querySelector('.modal-body'))
35 35
     $(modal).modal('show')
36
+
37
+  @handleDryRunButton: (button, data = $(button.form).serialize()) ->
38
+    $(button).prop('disabled', true)
39
+    $('body').css(cursor: 'progress')
40
+    $.ajax type: 'POST', url: $(button).data('action-url'), dataType: 'json', data: data
41
+      .always =>
42
+        $('body').css(cursor: 'auto')
43
+      .done (json) =>
44
+        Utils.showDynamicModal """
45
+          <h5>Log</h5>
46
+          <pre class="agent-dry-run-log"></pre>
47
+          <h5>Events</h5>
48
+          <pre class="agent-dry-run-events"></pre>
49
+          <h5>Memory</h5>
50
+          <pre class="agent-dry-run-memory"></pre>
51
+          """,
52
+          body: (body) ->
53
+            $(body).
54
+              find('.agent-dry-run-log').text(json.log).end().
55
+              find('.agent-dry-run-events').text(json.events).end().
56
+              find('.agent-dry-run-memory').text(json.memory)
57
+          title: 'Dry Run Results',
58
+          onHide: -> $(button).prop('disabled', false)
59
+      .fail (xhr, status, error) ->
60
+        alert('Error: ' + error)
61
+        $(button).prop('disabled', false)

+ 1 - 25
app/assets/javascripts/pages/agent-edit-page.js.coffee

@@ -142,31 +142,7 @@ class @AgentEditPage
142 142
 
143 143
   invokeDryRun: (e) ->
144 144
     e.preventDefault()
145
-    button = this
146
-    $(button).prop('disabled', true)
147
-    $('body').css(cursor: 'progress')
148
-    $.ajax type: 'POST', url: $(button).data('action-url'), dataType: 'json', data: $(button.form).serialize()
149
-      .always =>
150
-        $("body").css(cursor: 'auto')
151
-      .done (json) =>
152
-        Utils.showDynamicModal """
153
-          <h5>Log</h5>
154
-          <pre class="agent-dry-run-log"></pre>
155
-          <h5>Events</h5>
156
-          <pre class="agent-dry-run-events"></pre>
157
-          <h5>Memory</h5>
158
-          <pre class="agent-dry-run-memory"></pre>
159
-          """,
160
-          body: (body) ->
161
-            $(body).
162
-              find('.agent-dry-run-log').text(json.log).end().
163
-              find('.agent-dry-run-events').text(json.events).end().
164
-              find('.agent-dry-run-memory').text(json.memory)
165
-          title: 'Dry Run Results',
166
-          onHide: -> $(button).prop('disabled', false)
167
-      .fail (xhr, status, error) ->
168
-        alert('Error: ' + error)
169
-        $(button).prop('disabled', false)
145
+    Utils.handleDryRunButton(this)
170 146
 
171 147
 $ ->
172 148
   Utils.registerPage(AgentEditPage, forPathsMatching: /^agents/)

+ 6 - 3
app/controllers/agents_controller.rb

@@ -35,15 +35,18 @@ class AgentsController < ApplicationController
35 35
   end
36 36
 
37 37
   def dry_run
38
-    attrs = params[:agent]
38
+    attrs = params[:agent] || {}
39 39
     if agent = current_user.agents.find_by(id: params[:id])
40 40
       # PUT /agents/:id/dry_run
41
-      type = agent.type
41
+      if attrs.present?
42
+        type = agent.type
43
+        agent = Agent.build_for_type(type, current_user, attrs)
44
+      end
42 45
     else
43 46
       # POST /agents/dry_run
44 47
       type = attrs.delete(:type)
48
+      agent = Agent.build_for_type(type, current_user, attrs)
45 49
     end
46
-    agent = Agent.build_for_type(type, current_user, attrs)
47 50
     agent.name ||= '(Untitled)'
48 51
 
49 52
     if agent.valid?

+ 6 - 0
app/views/agents/_action_menu.html.erb

@@ -5,6 +5,12 @@
5 5
     </li>
6 6
   <% end %>
7 7
 
8
+  <% if agent.can_dry_run? %>
9
+    <li>
10
+      <%= link_to icon_tag('glyphicon-refresh') + ' Dry Run', '#', 'data-action-url' => dry_run_agent_path(agent), tabindex: "-1", onclick: "Utils.handleDryRunButton(this, '_method=PUT')" %>
11
+    </li>
12
+  <% end %>
13
+
8 14
   <li>
9 15
     <%= link_to icon_tag('glyphicon-eye-open') + ' Show'.html_safe, agent_path(agent) %>
10 16
   </li>